package com.example.jikeprofiledemo

import android.app.Activity
import android.graphics.Color
import android.os.Build
import android.os.Build.VERSION.SDK_INT
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.widget.NestedScrollView
import androidx.fragment.app.FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
import com.example.jikeprofiledemo.RefreshState.REVEALING
import com.example.jikeprofiledemo.RefreshState.SWITCHING
import com.example.jikeprofiledemo.dummy.DummyContent
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(), ItemFragment.OnListFragmentInteractionListener {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        stretchContentInStatusBar(this)

        viewPager.adapter = FPagerAdapter(
            supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, arrayListOf(
                ItemFragment.newInstance(1), ItemFragment.newInstance(1)
            ), arrayListOf("动态", "档案")
        )

        tabLayout.setupWithViewPager(viewPager)
    }

    override fun onResume() {
        super.onResume()

        //mock progress change
//        mockIndicatorProgressChange()
        mockAddUpHeight()
    }

    private fun mockAddUpHeight() {
        Thread {

            for (i in 0 until 50) {
                container.post {
                    val layoutParams = container.layoutParams as ViewGroup.LayoutParams
                    layoutParams.height += 15
                    container.layoutParams = layoutParams
                }
                Thread.sleep(100)
            }

        }.start()
    }

    private fun mockIndicatorProgressChange() {
        Thread {

            for (i in 0 until 101 step 10) {
                refreshIndicator.postOnAnimation {
                    refreshIndicator.refreshState = REVEALING
                    refreshIndicator.updateProgress(i.toFloat() / 100)
                }
                Thread.sleep(100)
            }


            refreshIndicator.postOnAnimation {
                refreshIndicator.startRefresh()
            }


        }.start()
    }

    override fun onListFragmentInteraction(item: DummyContent.DummyItem?) {
        Toast.makeText(applicationContext, item.toString(), Toast.LENGTH_SHORT).show()
    }


    private fun stretchContentInStatusBar(activity: Activity) {
        if (SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
            val decorView = activity.window.decorView
            val option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            decorView.systemUiVisibility = option

            //状态栏透明
            activity.window.statusBarColor = Color.TRANSPARENT

        } else if (SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
            val localLayoutParams = activity.window.attributes
            localLayoutParams.flags = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS or localLayoutParams.flags
        }

    }

}
